package com.home.security.app.runner;

import com.home.common.constants.redis.RedisDateBase;
import com.home.common.redis.RedisUtil;
import com.home.security.app.config.KeyConfiguration;
import com.home.security.app.constants.RedisKeyConstants;
import com.home.security.app.token.RsaKeyHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Description: 初始化 JWT 密钥/公钥
 * Created by 王大宸 on 2020-12-11 11:03
 * Created with IntelliJ IDEA.
 */
@Component
public class AuthServerRunner implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(AuthServerRunner.class);

    @Autowired
    private KeyConfiguration keyConfiguration;

    @Override
    public void run(String... args) throws Exception {
        Boolean redisUserPriKey = RedisUtil.hasKey(RedisKeyConstants.REDIS_USER_PRI_KEY, RedisDateBase.DEFAULT_DATE_BASE);
        Boolean redisUserPubKey = RedisUtil.hasKey(RedisKeyConstants.REDIS_USER_PUB_KEY, RedisDateBase.DEFAULT_DATE_BASE);
        Boolean redisServicePriKey = RedisUtil.hasKey(RedisKeyConstants.REDIS_SERVICE_PRI_KEY, RedisDateBase.DEFAULT_DATE_BASE);
        Boolean redisServicePubKey = RedisUtil.hasKey(RedisKeyConstants.REDIS_SERVICE_PUB_KEY, RedisDateBase.DEFAULT_DATE_BASE);

        if (redisUserPriKey && redisUserPubKey && redisServicePriKey && redisServicePubKey) {
            keyConfiguration.setUserPriKey(RsaKeyHelper.toBytes(String.valueOf(RedisUtil.get(RedisKeyConstants.REDIS_USER_PRI_KEY, RedisDateBase.DEFAULT_DATE_BASE))));
            keyConfiguration.setUserPubKey(RsaKeyHelper.toBytes(String.valueOf(RedisUtil.get(RedisKeyConstants.REDIS_USER_PUB_KEY, RedisDateBase.DEFAULT_DATE_BASE))));
            logger.info("用户公匙/密匙初始化完成...");

            keyConfiguration.setServicePriKey(RsaKeyHelper.toBytes(String.valueOf(RedisUtil.get(RedisKeyConstants.REDIS_SERVICE_PRI_KEY, RedisDateBase.DEFAULT_DATE_BASE))));
            keyConfiguration.setServicePubKey(RsaKeyHelper.toBytes(String.valueOf(RedisUtil.get(RedisKeyConstants.REDIS_SERVICE_PUB_KEY, RedisDateBase.DEFAULT_DATE_BASE))));
        } else {
            logger.info("正在初始化用户公匙/密匙...");
            Map<String, byte[]> keyMap = RsaKeyHelper.generateKey(keyConfiguration.getUserSecret());
            keyConfiguration.setUserPriKey(keyMap.get(RedisKeyConstants.PRI));
            keyConfiguration.setUserPubKey(keyMap.get(RedisKeyConstants.PUB));
            RedisUtil.set(RedisKeyConstants.REDIS_USER_PRI_KEY, RsaKeyHelper.toHexString(keyMap.get(RedisKeyConstants.PRI)), RedisDateBase.DEFAULT_DATE_BASE);
            RedisUtil.set(RedisKeyConstants.REDIS_USER_PUB_KEY, RsaKeyHelper.toHexString(keyMap.get(RedisKeyConstants.PUB)), RedisDateBase.DEFAULT_DATE_BASE);
            logger.info("用户公匙/密匙初始化完成...");

            logger.info("正在初始化服务器公匙/密匙...");
            keyMap = RsaKeyHelper.generateKey(keyConfiguration.getServiceSecret());
            keyConfiguration.setServicePriKey(keyMap.get(RedisKeyConstants.PRI));
            keyConfiguration.setServicePubKey(keyMap.get(RedisKeyConstants.PUB));
            RedisUtil.set(RedisKeyConstants.REDIS_SERVICE_PRI_KEY, RsaKeyHelper.toHexString(keyMap.get(RedisKeyConstants.PRI)), RedisDateBase.DEFAULT_DATE_BASE);
            RedisUtil.set(RedisKeyConstants.REDIS_SERVICE_PUB_KEY, RsaKeyHelper.toHexString(keyMap.get(RedisKeyConstants.PUB)), RedisDateBase.DEFAULT_DATE_BASE);
        }
        logger.info("服务器公匙/密匙初始化完成...");
    }


}
